package com.hgg.controller;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;

/**
 * Created by hgg on 2018/7/16.
 */
@Controller
public class RabbitMqController {

    private final Logger logger = Logger.getLogger(RabbitMqController.class);
//    @Autowired
//    RabbitAdmin rabbitAdmin;

    @Autowired
    AmqpTemplate amqpTemplate;

    String queueName = "hgg1";



/*    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, "exchangemsxferror", "routingkeymsxferror");
    }*/


    @RequestMapping("/send")
    @ResponseBody
    public String send() throws Exception {
        String sendStr = "Hello 爸爸来了"+System.currentTimeMillis();

        logger.info(sendStr);
//        rabbitAdmin.getRabbitTemplate().convertAndSend(queueName,queueName,sendStr);
        amqpTemplate.convertAndSend(queueName,sendStr);

        return sendStr;
    }

    @RequestMapping("/sends")
    @ResponseBody
    public String sends() throws Exception {

        for (int i = 0; i < 20; i++){
            String sendStr = "爸爸来了"+System.currentTimeMillis();

//            logger.info(sendStr);
//        rabbitAdmin.getRabbitTemplate().convertAndSend(queueName,queueName,sendStr);
            amqpTemplate.convertAndSend(queueName,sendStr);
            amqpTemplate.convertAndSend("hgg2",sendStr);
        }
        return "succ";
    }


    @RequestMapping("/topic1")
    @ResponseBody
    public String  topicSend1() {
        String context = "my topic 1";
        System.out.println("baba说 : " + context);
        this.amqpTemplate.convertAndSend("exchange", "topic.message", context);
        return context;
    }
    @RequestMapping("/topic2")
    @ResponseBody
    public String topicSend2() {
        String context = "my topic 2";
        System.out.println("发送者说 : " + context);
        this.amqpTemplate.convertAndSend("exchange", "topic.messages", context);
        return  context;
    }



/*    @PostConstruct
    protected void declareBinding(){
        System.out.println("rabbitMq---declareBinding……");
        Queue queue = new Queue(queueName, true, false, false, null);
        rabbitAdmin.declareQueue(queue);
        TopicExchange topicExchange = new TopicExchange(queueName);
        rabbitAdmin.declareExchange(topicExchange);
        Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(queueName);
        rabbitAdmin.declareBinding(binding);
    }*/



    public static void main(String[] args) {

    }

}
